home *** CD-ROM | disk | FTP | other *** search
/ CICA 1993 April / CICA MS Windows - April 1993.iso / unzipped / programr / textview / demo / copy.c next >
C/C++ Source or Header  |  1991-06-14  |  8KB  |  262 lines

  1. /*****************************************************************************
  2.  
  3.     copy.c
  4.     ------
  5.  
  6.     Routines in this module demonstrate how the TVReturnData function is
  7.     used, by copying the trace window contents to another TextView window
  8.  
  9.     This source is Copyright (c) Alan Phillips 1991. It may be freely used
  10.     and adapted for non-commercial applications. Commercial and ShareWare
  11.     authors should first obtain the written permission of the author.
  12.  
  13.     The source is edited with a tab size of 4.
  14.  
  15. *****************************************************************************/
  16.  
  17. #include    "stdhead.h"
  18.  
  19.  
  20. /*****************************************************************************
  21.  
  22.     Local Data
  23.     ----------
  24.  
  25. *****************************************************************************/
  26.  
  27. static    HWND    hCopyWnd;                /* handle to copy window */
  28. static    FARPROC    lpCopyMenuHandler;        /* instance of menu handler function */
  29.  
  30. /*****************************************************************************
  31.  
  32.     copy_trace_window
  33.     -----------------
  34.  
  35.     This routine is called from the 'copy' option of the main window's
  36.     'Trace' menu. It creates a second TextView window (or, if it already
  37.     exists, empties it) and uses the TVReturnData function to copy the
  38.     current contents of the trace window into it
  39.  
  40.     copy_trace_window(hWnd)
  41.  
  42.     HWND    hWnd;                    Handle to main window
  43.  
  44. *****************************************************************************/
  45.  
  46. void    copy_trace_window(HWND hWnd)
  47.  
  48. {
  49.     char    buffer[256];                /* buffer for reading lines from the
  50.                                         *  trace window into
  51.                                         */
  52.     FARPROC    lpNotifyProc;                /* ptr to notification proc */
  53.     
  54.     /* If we don't have a copy window at the moment, we'll create one using the
  55.     *  same window class as the trace window. If we do have one already we
  56.     *  empty it and make sure it's visible
  57.     */
  58.  
  59.     if ( hCopyWnd != NULL )
  60.     {
  61.         /* We already have a copy window, so we'll reset it and make sure
  62.         *  that it's visible
  63.         */
  64.  
  65.         TVResetWindow(hCopyWnd);
  66.         if ( IsIconic(hCopyWnd) )
  67.             ShowWindow(hCopyWnd,SW_SHOWNORMAL);
  68.         else
  69.             BringWindowToTop(hCopyWnd);
  70.     }
  71.     else
  72.     {
  73.         /* We don't have a copy window, so we'll create one. Since we don't
  74.         *  have an option in the application's menu to close this window,
  75.         *  we need to give it a system menu and leave the 'Close' option
  76.         *  active. This means that we have to have a callback function so that
  77.         *  TextView can notify us when the window closes.
  78.         *
  79.         *  Note that we don't include the TVS_TIMESTAMP option for this
  80.         *  window. The lines we read back from the trace window will include
  81.         *  the timestamp value added by TextView when they were written.
  82.         *
  83.         *  We make the window's storage buffer 2 lines larger than the trace
  84.         *  window's, so we can add a header and trailer line. If the trace
  85.         *  window is at the maximum size, this request will be rounded down.
  86.         */
  87.  
  88.         lpCopyMenuHandler    = MakeProcInstance(copy_menu_handler,hInst);
  89.  
  90.         hCopyWnd    = TVCreateWindow("TRACE_WINDOW",
  91.                                      "Trace Copy",
  92.                                      CW_USEDEFAULT,
  93.                                      CW_USEDEFAULT,
  94.                                      300,
  95.                                      200,
  96.                                      hInst,
  97.                                      NULL,
  98.                                      TVS_VSCROLL | TVS_HSCROLL |
  99.                                         TVS_SYSMENU | TVS_SCROLLMENU,
  100.                                      4,
  101.                                      0,
  102.                                      TRW_SIZE+2,
  103.                                      lpCopyMenuHandler);
  104.  
  105.         if ( hCopyWnd == NULL )
  106.         {
  107.             /* The handle we got back was NULL, so something has gone wrong and
  108.             *  we don't have the window
  109.             */
  110.  
  111.             message("Cannot create copy window",NULL,MB_ICONSTOP);
  112.             return;
  113.         }
  114.     }
  115.  
  116.     /* The window is created, so we can write a message to head it, writing
  117.     *  it in blue
  118.     */
  119.  
  120.     TVSetTextColor(hCopyWnd,RGB(0,0,255));
  121.     TVOutputText(hCopyWnd,"*** Copy of trace window ***",0);
  122.  
  123.     /* Then put the colour for the rest of the text to be black */
  124.  
  125.     TVSetTextColor(hCopyWnd,RGB(0,0,0));
  126.  
  127.     /* Since we may have to write a large number of lines to the copy window,
  128.     *  we'll now mark it so that the screen isn't updated every time we write a
  129.     *  line. That way we'll finish much faster, since screen output and
  130.     *  scrolling accounts for most of the time taken
  131.     */
  132.  
  133.     TVSetRedraw(hCopyWnd,FALSE);
  134.  
  135.     /* And now we can do the real work of reading back the lines in the
  136.     *  trace window. We first need to set up a procedure instance of the
  137.     *  notify function that TextView will use to tell us every time it
  138.     *  copies a line into our buffer
  139.     */
  140.  
  141.     lpNotifyProc    = MakeProcInstance(notify_handler,hInst);
  142.  
  143.     /* And then we ask TextView for the data. The work is then done inside
  144.     *  the notification handler, and we won't return from TVReturnData until
  145.     *  it's all done.
  146.     */
  147.  
  148.     TVReturnData(hTraceWnd,buffer,sizeof(buffer),lpNotifyProc);
  149.  
  150.     /* When we get to here the read out process has ended, so we can free off
  151.     *  the procedure instance to the notification handler
  152.     */
  153.  
  154.     FreeProcInstance(lpNotifyProc);
  155.  
  156.     /* We can now allow the output window to be redrawn again. The change from
  157.     *  redrawing off to redrawing on will make TextView repaint the window so
  158.     *  we'll see that last screen-full of lines
  159.     */
  160.  
  161.     TVSetRedraw(hCopyWnd,TRUE);
  162.  
  163.     /* Write a message to the copy window to show that that's the end; we
  164.     *  do this in blue
  165.     */
  166.  
  167.     TVSetTextColor(hCopyWnd,RGB(0,0,255));
  168.     TVOutputText(hCopyWnd,"*** Copy Completed ***",0);
  169.  
  170.     /* Then put the copy window into manual scroll mode and scroll it back so
  171.     *  that the first line is visible
  172.     */
  173.  
  174.     TVSetScrollState(hCopyWnd,TV_SCR_MANUAL);
  175.     TVSetVPosition(hCopyWnd,TVSP_START);
  176.  
  177. }
  178.  
  179.  
  180. /*****************************************************************************
  181.  
  182.     copy_menu_handler
  183.     -----------------
  184.  
  185.     This is the menu handler callback function specified in the call to
  186.     TVCreateWindow that creates the copy of the trace window. Here we're
  187.     interested only in monitoring when the user closes the window so that
  188.     we can clear out our copy of the handle and release resources.
  189.  
  190.     This routine must be exported in the appliation's module definition file.
  191.     It must use the Pascal calling convention, and be declared FAR.
  192.  
  193.     copy_menu_handler(hWnd,nMenuItem)
  194.  
  195.     HWND    hWnd;                    Handle to window
  196.     WORD    nMenuItem;                Code indicating the menu item
  197.  
  198. *****************************************************************************/
  199.  
  200. void    FAR    PASCAL    copy_menu_handler(HWND hWnd,WORD nMenuItem)
  201.  
  202. {
  203.     /* We look only for the system menu close option being clicked */
  204.  
  205.     if ( nMenuItem == TVMI_CLOSE )
  206.     {
  207.         /* The user has closed the window through the system menu. Lose our
  208.         *  copy of the handle
  209.         */
  210.  
  211.         hCopyWnd    = NULL;
  212.  
  213.         /* And free the procedure instance of this function */
  214.  
  215.         FreeProcInstance(lpCopyMenuHandler);
  216.     }
  217.  
  218. }
  219.  
  220.  
  221. /*****************************************************************************
  222.  
  223.     notify_handler
  224.     --------------
  225.  
  226.     This is the notification handler specified in the call to TVReturnData.
  227.     TextView will call this routine every time it places the text of a line
  228.     from the trace window into our buffer; we can then process the buffer as
  229.     we wish. TextView will keep calling back into this routine until either
  230.     all the text has been passed back, or we return a zero value.
  231.  
  232.     This routine must be exported in the appliation's module definition file.
  233.     It must use the Pascal calling convention, and be declared FAR.
  234.  
  235.     reply = notify_handler(hWnd,lpBuffer,nCount,nTruncated)
  236.  
  237.     int        reply;            0 to stop the callback process
  238.     HWND    hWnd;            Handle to the window whose data is being read
  239.     LPSTR    lpBuffer;        Ptr to our buffer
  240.     int        nCount;            Number of this line in the window (the first line
  241.                             is numbered zero)
  242.     BOOL    nTruncated;        TRUE if text was truncated to fit our buffer
  243.  
  244. *****************************************************************************/
  245.  
  246. int        FAR    PASCAL    notify_handler(HWND hWnd,LPSTR lpBuffer,int nCount,
  247.                                    BOOL nTruncated)
  248.  
  249. {
  250.     /* All we do here is to write the current contents of the buffer back out
  251.     *  to the copy of the trace window. We don't care what's in it or whether
  252.     *  it's been truncated
  253.     */
  254.  
  255.     TVOutputText(hCopyWnd,lpBuffer,lstrlen(lpBuffer));
  256.  
  257.     /* And return a non-zero value to continue with the next line */
  258.  
  259.     return(1);
  260.  
  261. }
  262.